<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="采购单号">
              <a-input placeholder="请输入采购单号" v-model="queryParam.purchaseNub"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="采购名称">
              <a-input placeholder="请输入采购名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="采购部门">
                <j-select-depart placeholder="请选择采购部门" v-model="queryParam.procurementDepartment"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('采购管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
                     @handleSuperQuery="handleSuperQuery"></j-super-query>
      <!--      <a-dropdown v-if="selectedRowKeys.length > 0">
              <a-menu slot="overlay">
                <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
              </a-menu>
              <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
            </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap1"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="purchaseNub" slot-scope="text,record">
          <a v-html="text" @click="handleDetailDrawer(record)"></a>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="state" slot-scope="text, record">
          <span v-if="record.state==2" style="color: green">{{ text }}</span>
          <span v-else-if="record.state==3" style="color: red">{{ text }}</span>
          <span v-else-if="record.state==1" style="color: blue">{{ text }}</span>
          <span v-else>{{ text }}</span>
        </span>
        <span slot="action" slot-scope="text, record">
        <div v-if="record.state==0">
           <a @click="submitApplication(record)">提交审批</a>

          <a-divider type="vertical"/>
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>

          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
          <!--          <a-dropdown>
                      <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                      <a-menu slot="overlay">
                        <a-menu-item>
                          <a @click="handleDetail(record)">详情</a>
                        </a-menu-item>
                        <a-menu-item>
                          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                            <a>删除</a>
                          </a-popconfirm>
                        </a-menu-item>
                      </a-menu>
                    </a-dropdown>-->
          </div>

         <div v-if="record.state==3">
           <a @click="handleReissue(record)">重新发起</a>
         </div>
        </span>

      </a-table>
    </div>

    <ene-purchasing-management-modal ref="modalForm" @ok="modalFormOk"/>
    <TabsDrawer @changeTabs="changeTabs" :width="1400" ref="tabsDrawer">
      <template slot="tabs-0">
        <collapse-comp @initData="initData" ref="collapsRef"></collapse-comp>
      </template>
      <template slot="tabs-1">
        <div>
          <a-input-search placeholder="备品备件名称" style="width: 200px;margin-bottom: 20px" @search="onSearch"/>
          <a-table
            class="bpbj"
            :rowKey="id"
            :columns="bpbjColumns"
            :dataSource="bpbjDataSource"
            :pagination="false"
          >
            <template slot="name" slot-scope="text, record">


            </template>
            <!--         <span slot="action" slot-scope="text, record">
                      <a @click="delKuwei(record)">删除</a>
                     </span>-->
            <template slot="footer" slot-scope="currentPageData">
              金额合计（元）：<span class="heji">{{ bpbjHeji }}</span>
            </template>
          </a-table>

        </div>
      </template>
      <template slot="tabs-2">
        <a-table :rowKey="id"
                 :columns="fujianColumns"
                 :dataSource="fujianDataSource">
           <span slot="action" slot-scope="text, record">
<!--             预览没写，等  file-view-domain-->
<!--        <a href="javascript:;" @click="handlePreview(record.adress+record.name+'.'+record.type)">预览</a>-->
<!--        <a-divider type="vertical"/>-->
              <a href="javascript:;" @click="downloadFile(record.adress+record.name+'.'+record.type)">下载</a>
<!--              <a-divider type="vertical"/>-->
<!--               <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">-->
<!--                  <a>删除</a>-->
<!--                </a-popconfirm>-->
      </span>
        </a-table>
      </template>
    </TabsDrawer>
  </a-card>
</template>

<script>

import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import EnePurchasingManagementModal from './modules/EnePurchasingManagementModal'
import TabsDrawer from '@/components/Drawer/TabsDrawer'
import CollapseComp from '@/components/Collapse/CollapsComp'
import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import {getAction, getFileAccessHttpUrl} from "@api/manage";
import {JVXETypes} from "@comp/jeecg/JVxeTable";

const bpbjColumns = [
  {
    title: '备品备件名称',
    align: "center",
    dataIndex: 'name'
  },
  {
    title: '备品备件编号',
    align: "center",
    dataIndex: 'number'
  },
  {
    title: '规格型号',
    align: "center",
    dataIndex: 'model'
  },
  {
    title: '单位',
    align: "center",
    dataIndex: 'unit_dictText'
  },
  {
    title: '数量',
    align: "center",
    dataIndex: 'quantity',

  },
  {
    title: '采购单价（元）',
    align: "center",
    dataIndex: 'price',

  },
  {
    title: '备注',
    align: "center",
    dataIndex: 'remarks',

  },
]
const fujianColumns = [
  {
    title: '文件路径',
    align: "center",
    dataIndex: 'adress'
  },
  {
    title: '文件名',
    align: "center",
    dataIndex: 'name'
  },
  {
    title: '类型',
    align: "center",
    dataIndex: 'type'
  },
/*  {
    title: '文件大小',
    align: "center",
    dataIndex: 'name'
  },
  {
    title: '上传人',
    align: "center",
    dataIndex: 'name'
  },
  {
    title: '上传时间',
    align: "center",
    dataIndex: 'name'
  },*/
  {
    title: '操作',
    dataIndex: 'action',
    align: "center",
    scopedSlots: {customRender: 'action'}
  }
]

export default {
  name: "EnePurchasingManagementList",
  mixins: [JeecgListMixin],
  components: {
    EnePurchasingManagementModal,
    TabsDrawer,
    CollapseComp
  },
  data() {
    return {
      description: '采购管理管理页面',
      // 表头
      bpbjColumns,
      fujianColumns,
      bpbjDataSource: [],
      fujianDataSource:[],
      bpbjDataSourceCopy:[],
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '采购单号',
          align: "center",
          dataIndex: 'purchaseNub',
          scopedSlots: {customRender: 'purchaseNub'},
        },
        {
          title: '采购名称',
          align: "center",
          dataIndex: 'name'
        },
        {
          title: '采购种类',
          align: "center",
          dataIndex: 'typeNub'
        },
        {
          title: '采购金额（元）',
          align: "center",
          dataIndex: 'money'
        },
        {
          title: '采购部门',
          align: "center",
          dataIndex: 'procurementDepartment_dictText'
        },
        {
          title: '采购人',
          align: "center",
          dataIndex: 'purchasingPerson_dictText'
        },
        {
          title: '采购日期',
          align: "center",
          dataIndex: 'purchaseDate',
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '备注',
          align: "center",
          dataIndex: 'remarks'
        },
        {
          title: '创建人',
          align: "center",
          dataIndex: 'createBy_dictText'
        },
        {
          title: '创建日期',
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '状态',
          align: "center",
          dataIndex: 'state_dictText',
          scopedSlots: {customRender: 'state'},
        },
        {
          title: '审批时间',
          align: "center",
          dataIndex: 'approvalTime'
        },
        {
          title: '审批原因',
          align: "center",
          dataIndex: 'reason'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'},
        }
      ],
      url: {
        list: "/ene/enePurchasingManagement/list",
        delete: "/ene/enePurchasingManagement/delete",
        deleteBatch: "/ene/enePurchasingManagement/deleteBatch",
        exportXlsUrl: "/ene/enePurchasingManagement/exportXls",
        importExcelUrl: "ene/enePurchasingManagement/importExcel",
        submit: "ene/enePurchasingManagement/submit",
        enePurchasingSpareparts: '/ene/enePurchasingManagement/queryEnePurchasingSparepartsByMainId'
      },
      dictOptions: {},
      superFieldList: [],
      selectedRecord: {},
      bpbjHeji: 0,
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  methods: {
    handleCancel(){
      this.previewVisible = false;
    },
    handlePreview(file){

    },
    onSearch(value){
      if(value==""||value==null){
        this.bpbjDataSource=[...this.bpbjDataSourceCopy]
      }else{
        this.bpbjDataSource=this.bpbjDataSourceCopy.filter(e=>{
          return e.name.indexOf(value)!=-1
        })
      }

    },
    changeTabs(value) {
      if (value == '1') {
        getAction(this.url.enePurchasingSpareparts, {id: this.selectedRecord.id}).then(res => {
          this.bpbjDataSource = res.result
          this.bpbjDataSourceCopy=res.result
          this.bpbjHeji = res.result.reduce((acc, obj) => acc + obj.quantity * obj.price, 0);
        })
      }
      if(value == '2'){
        this.fujianDataSource=this.selectedRecord.attachment.split(',').map(res=>(
          {
            adress:res.split('/')[0]+'/',
            name:res.split('/')[1].split('.')[0],
            type:res.split('.')[1]
          }))
      }
    },
    handleDetailDrawer(record) {
      this.$refs.tabsDrawer.record = record;
      this.$refs.tabsDrawer.topData = [
        {
          name: '采购名称', value: record.name, lg: 3
        },
        {
          name: '采购单号', value: record.purchaseNub, lg: 3
        },
        {
          name: '采购部门', value: record.procurementDepartment_dictText, lg: 3
        },
        {
          name: '采购种类', value: record.typeNub, lg: 3
        },
        {
          name: '采购金额（元）', value: record.money, lg: 3
        },
        {
          name: '提报人', value: record.purchasingPerson_dictText, lg: 3
        },
        {
          name: '提报时间', value: record.purchaseDate, lg: 3
        }

      ];
      this.$refs.tabsDrawer.tabsData = [
        {
          title: '基础信息'
        },
        {
          title: '备品备件'
        },
        {
          title: '附件'
        },
      ]
      this.selectedRecord = record

      this.$refs.tabsDrawer.visible = true

    },
    initData() {
      const record = this.selectedRecord
      this.$refs.collapsRef.collapseData = [
        {
          title: '基础信息',
          children: [
            {
              label: '采购名称',
              value: record.name
            },
            {
              label: '采购单号',
              value: record.purchaseNub
            },
            {
              label: '采购种类',
              value: record.typeNub
            },
            {
              label: '采购金额',
              value: record.money
            },
            {
              label: '采购部门',
              value: record.money
            },
            {
              label: '采购人',
              value: record.purchasingPerson_dictText
            },
            {
              label: '采购日期',
              value: record.purchaseDate
            },
            {
              label: '当前状态',
              value: record.state_dictText
            },
            {
              label: '备注',
              value: record.remarks
            }
          ]
        },
        {
          title: '审批信息',
          children: [
            {
              label: '审批人',
              value: record.updateBy
            },
            {
              label: '审批时间',
              value: record.updateTime
            },
            {
              label: '审批说明',
              value: record.reason
            },
          ]
        },
        {
          title: '系统信息',
          children: [
            {
              label: '创建人',
              value: record.createBy_dictText
            },
            {
              label: '创建时间',
              value: record.createTime
            },
            {
              label: '修改人',
              value: record.updateBy
            },
            {
              label: '修改时间',
              value: record.updateTime
            },
          ]
        }
      ]
    },
    handleReissue: function (record) {
      this.$refs.modalForm.edit(record);
      this.$refs.modalForm.title = "重新发起";
      this.$refs.modalForm.disableSubmit = false;
    },
    submitApplication(record) {
      getAction(this.url.submit, {id: record.id}).then((res) => {
        if (res.success) {
          this.$message.success('提交成功')
        }
      }).finally(() => {
        this.loadData()
      })
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({type: 'string', value: 'purchaseNub', text: '采购单号', dictCode: ''})
      fieldList.push({type: 'string', value: 'name', text: '采购名称', dictCode: ''})
      fieldList.push({type: 'sel_depart', value: 'procurementDepartment', text: '采购部门'})
      fieldList.push({type: 'sel_user', value: 'purchasingPerson', text: '采购人'})
      fieldList.push({type: 'date', value: 'purchaseDate', text: '采购日期'})
      fieldList.push({type: 'string', value: 'remarks', text: '备注', dictCode: ''})
      fieldList.push({type: 'Text', value: 'attachment', text: '采购附件', dictCode: ''})
      fieldList.push({type: 'sel_user', value: 'createBy', text: '创建人'})
      fieldList.push({type: 'datetime', value: 'createTime', text: '创建日期'})
      fieldList.push({type: 'string', value: 'state', text: '状态', dictCode: 'order_status'})
      fieldList.push({type: 'datetime', value: 'approvalTime', text: '审批时间'})
      fieldList.push({type: 'string', value: 'reason', text: '拒绝原因', dictCode: ''})
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

.heji {
  font-weight: bolder;
}

.bpbj /deep/ .ant-table-footer {
  text-align: right;
}
</style>